﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web_cuiki
{
    public class TacGiaDAO
    {
        public static List<TAC_GIA> laydsTacGia()
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<TAC_GIA> ds = null;
            var query = from ksp in db.TAC_GIAs
                        select ksp;
            ds = query.ToList<TAC_GIA>();
            return ds;
        }
        public static int XoaTheoMa(int maKM)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            try
            {
                var query = from hd in db.TAC_GIAs where hd.MaTacGia == maKM select hd;
                TAC_GIA hoadon = query.First<TAC_GIA>();
                System.Data.Linq.Table<TAC_GIA> table = db.GetTable<TAC_GIA>();
                table.DeleteOnSubmit(hoadon);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static TAC_GIA TimTacGiaTheoMa(int maTacGia)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();

            var query = from hd in db.TAC_GIAs
                        where hd.MaTacGia == maTacGia
                        select hd;

            return query.First<TAC_GIA>();

        }
        public static TAC_GIA TimTacGiaTheoTen(string ten)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();

            var query = from hd in db.TAC_GIAs
                        where hd.TenTacGia == ten
                        select hd;

            return query.First<TAC_GIA>();

        }
        public static int ThemTacGia(string ten, string hinh, string gioithieu)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<TAC_GIA> table = db.GetTable<TAC_GIA>();
            try
            {
                TAC_GIA sp = new TAC_GIA();
                if (ten != null)
                    sp.TenTacGia = ten;
                if (hinh != null)
                    sp.HinhAnh = hinh;
                if (gioithieu != null)
                    sp.GioiThieu = gioithieu;
                table.InsertOnSubmit(sp);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return kq;

        }
    }
}